package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalPostGradeLevelDao;
import com.boss.salary.entity.SalPostGradeLevel;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SalPostGradeLevelDaoImpl extends BaseDaoImpl implements ISalPostGradeLevelDao {

    @Override
    public List<Map<String, Object>> querySalPostGradeLevelPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.id,a.person_type_id personTypeId,b.name personTypeName,a.post_id postId,c.name postName, ")
                .append(" a.code,a.name,a.type,a.create_time createTime,a.update_time updateTime from ")
                .append(TableName.SAL_POST_GRADE_LEVEL)
                .append(" a  JOIN ").append(TableName.SAL_PERSON_TYPE)
                .append(" b on a.person_type_id = b.id  JOIN ").append(TableName.SAL_POST)
                .append(" c on a.post_id = c.id where 1=1 ");
        if (!StringUtils.isEmpty(queryParam.get("type"))) {
            sql.append(" and a.type = ? ");
            paramList.add(queryParam.get("type"));
        }
        sql.append(" order by a.code is null,a.code ");
        String querySql = this.boxUpSql(sql.toString());
        Object[] params = boxUpParam(paramList.toArray(), paginationDTO);
        return this.getjdbcTemplate().queryForList(querySql, params);
    }

    @Override
    public List<Map<String, Object>> querySalPostGradeLevelAll(Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.id,a.person_type_id personTypeId,b.name personTypeName,a.post_id postId,c.name postName, ")
                .append(" a.code,a.name,a.type,a.create_time createTime,a.update_time updateTime from ")
                .append(TableName.SAL_POST_GRADE_LEVEL)
                .append(" a  JOIN ").append(TableName.SAL_PERSON_TYPE)
                .append(" b on a.person_type_id = b.id  JOIN ").append(TableName.SAL_POST)
                .append(" c on a.post_id = c.id where 1=1 ");
        if (!StringUtils.isEmpty(queryParam.get("personTypeId"))) {
            sql.append(" and a.person_type_id = ? ");
            paramList.add(queryParam.get("personTypeId"));
        }
        if (!StringUtils.isEmpty(queryParam.get("postId"))) {
            sql.append(" and a.post_id = ? ");
            paramList.add(queryParam.get("postId"));
        }
        if (!StringUtils.isEmpty(queryParam.get("type"))) {
            sql.append(" and a.type = ? ");
            paramList.add(queryParam.get("type"));
        }
        sql.append(" order by a.code ");
        return this.getjdbcTemplate().queryForList(sql.toString(), paramList.toArray());
    }

    @Override
    public int queryDataByCode(String type, String code) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT COUNT(*) FROM ").append(TableName.SAL_POST_GRADE_LEVEL);
        sql.append(" WHERE code = ? and type = ? ");
        return this.getjdbcTemplate().queryForObject(sql.toString(), new Object[]{code, type}, Integer.class);
    }

    @Override
    public int querySalPostGradeLevelCount(Map<String, Object> queryParam) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT count(*) FROM ").append(TableName.SAL_POST_GRADE_LEVEL)
                .append(" a  JOIN ").append(TableName.SAL_PERSON_TYPE)//LEFT
                .append(" b on a.person_type_id = b.id  JOIN ").append(TableName.SAL_POST)//LEFT
                .append(" c on a.post_id = c.id where 1=1 ");
        if (!StringUtils.isEmpty(queryParam.get("type"))) {
            sql.append(" and a.type = ? ");
            paramList.add(queryParam.get("type"));
        }
        sql.append(" order by a.create_time ");
        return this.getjdbcTemplate().queryForObject(sql.toString(), paramList.toArray(), Integer.class);
    }

    @Override
    public void insertSalPostGradeLevel(SalPostGradeLevel salPostGradeLevel) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert into ").append(TableName.SAL_POST_GRADE_LEVEL);
        sql.append(" (id, create_time, update_time, person_type_id, post_id, code, name, type) ")
                .append(" value (?,?,?,?,?,?,?,?) ");
        Object[] params = new Object[]{
                salPostGradeLevel.getId(), salPostGradeLevel.getCreateTime(), salPostGradeLevel.getUpdateTime(),
                salPostGradeLevel.getPersonTypeId(), salPostGradeLevel.getPostId(), salPostGradeLevel.getCode(),
                salPostGradeLevel.getName(), salPostGradeLevel.getType()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateSalPostGradeLevel(SalPostGradeLevel salPostGradeLevel) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.SAL_POST_GRADE_LEVEL);
        sql.append(" set create_time=?, update_time=?, person_type_id=?, post_id=?, code=?, name=?, type=? ")
                .append(" where id = ? ");
        Object[] params = new Object[]{
                salPostGradeLevel.getCreateTime(), salPostGradeLevel.getUpdateTime(),
                salPostGradeLevel.getPersonTypeId(), salPostGradeLevel.getPostId(), salPostGradeLevel.getCode(),
                salPostGradeLevel.getName(), salPostGradeLevel.getType(), salPostGradeLevel.getId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deleteSalPostGradeLevel(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from ").append(TableName.SAL_POST_GRADE_LEVEL)
                .append(" where id=? ");
        Object[] params = new Object[]{id};
        this.getJdbcTemplate().update(sql.toString(), params);
    }

}